package team.tunan.mapper;

import org.apache.ibatis.annotations.*;
import team.tunan.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author sqx
 * @since 2024-01-01
 */
public interface UserMapper extends BaseMapper<User> {
    /*
     * 登录
     */
    @Select("select * from user where email=#{account} and password = #{password}")
    User getUsertoLogin(@Param("account") String account, @Param("password") String password);

    /**
     * 比赛未开始 搜索所有没参赛的选手进行参赛
     * @return
     */
    @Select("select id,username from user where role = 3 and player_select = 0")
    List<Map<String,Object>> getPlayInfo();

    /**
     * 开启下一轮场次是就只会搜索出选手为晋级状态的人
     * @param
     * @return
     */
    @Select("select id,username from user where role = 3 and player_select = 2 and matchId = #{gid} ")
    List<Map<String,Object>> getPlayInfoTwo(Integer gid);


    /*
     * 新增单个用户
     */
    int insertUser(@Param("user") User user);

    /*
     * 批量新增用户
     */
    int addBatch(List<User> users);

    /*
     * 删除单个用户
     */
    @Delete("delete from user where id = #{id}")
    int deleteById(Integer id);

    /*
     * 用户修改用户信息
     */
    int updateUserByid(User user);

    /*
     * 根据id查询用户
     */
    @Select("select * from user where id = #{id}")
    User getUserById(Integer id);

    /*
     * 查询所有用户
     */
    @Select("select * from user")
    List<User> getAllUser();


    /*
     * 查询选手
     */
    @Select("select * from user where role = 3")
    List<User> getPlayers();


    /*
     * 更改选手状态 是非被选中
     */
    @Update("update user set player_select = 1 ,matchId = #{matchId},roundNum = roundNum+1 where id =#{pid}")
    int updatePlayerStatusById(@Param("matchId") Integer matchId,@Param("pid") Integer pid);


    /**
     * 判断上一轮选手是否都已经结束比赛
     * @param matchId
     * @param roundNum
     * @return
     */
    @Select("select  * from user where matchId = #{matchId} and player_select = 1 and roundNum = #{roundNum}")
    List<User> isEnd(@Param("matchId") Integer matchId, @Param("roundNum") Integer roundNum);

    @Update("update user set player_select = 0 ,matchId = 0,roundNum = 0 where id =#{pid}")
    int updatePlayerEndById(@Param("pid") Integer pid);

    @Update("update user set  player_select = 2 where id = #{id}")
    int updatePromote(@Param("id") Integer id);

}
